<!--
* description: 备件清单维护
* author: xiaochengzuan
* createdDate: 2021-3-2
-->
<template>
  <section class="editBigAreaInfo">
    <el-dialog
      v-dialogDrag
      :close-on-click-modal="false"
      :title="textMap[popupsState]"
      :append-to-body="true"
      :visible.sync="curPopupsVisible"
      width="70%"
      @close="closeEdit"
    >
      <dialogHeader
        slot="title"
        :title="textMap[popupsState]"
        :style-type="$store.state.app.dialogType"
      />

      <div class="filter-container filter-params">
        <el-row ref="vailComponentList" :gutter="26">
          <component
            :is="comp.component"
            v-for="comp in tableComponents.filter((o) => o.isMust === true)"
            :ref="comp.isRequire ? comp.isRequire + comp.compKey : comp.compKey"
            :key="comp.compKey"
            :validrule="comp.validrule"
            :code-field="comp.codeField"
            :text-field="comp.textField"
            :popups-key="comp.compKey"
            :parent-fileds="comp.parentFileds"
            :code="formField[comp.codeField]"
            :text="formField[comp.textField]"
            :disabled="comp.disabled"
            :o-fields="comp.oFields"
            :options="comp.options"
            :data-to-object="comp.dataToObject"
            :custom-obj="comp.customObj"
            :is-require="comp.isRequire"
            :is-mul="comp.isMul"
            :span="comp.span || 8"
            :label-name="comp.labelName"
            :lookuptype="comp.lookuptype"
            :date-options-type="comp.dateOptionsType"
            @changeCode="getComponentCode"
            @focusCode="getFocusCode"
          />
        </el-row>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button
          v-for="comp in tableButtons"
          :key="comp.compKey"
          :type="comp.type"
          :icon="comp.icon"
          @click="comp.clickEvent"
        >{{ comp.text }}</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import { formMixins } from '@/components/mixins/formMixins'

import { orgApis } from '@/api/pabasicdataapi'

export default {
  components: {
    dialogHeader: () => import('@/components/dialogHeader'),
    mixButton: () => import('@/components/basicComponent/mixButton')
  },

  // 组件混入对象
  mixins: [formMixins],

  props: {
    popupsVisible: { type: Boolean, default: false },
    // 从网格传入的编辑行数据(json)
    dynamicEditRowData: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },

  data() {
    const viewPageCode = 'bigAreaInfo-edit'
    return {
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          icon: 'iconfont  icon-preservation',
          clickEvent: () => this.save(),
          text: '保存' /* 保存*/,
          position: 'right'
        },
        {
          compKey: 'btnKey2',
          type: '',
          size: 'small',
          icon: 'iconfont  icon-cancel',
          clickEvent: () => this.closeEdit(),
          text: this.$t('sys.button.cancel') // 取消
        }
      ],

      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode] &&
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode].formComponents
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[viewPageCode].formComponents
          : [
            {
              compKey: 'compKey1',
              labelName: '备件编码：',
              codeField: 'partNo',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isRequire: true,
              isMust: true,
              disabled: true
            },

            {
              compKey: 'compKey2',
              labelName: '备件名称：',
              codeField: 'partName',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isRequire: true,
              isMust: true,
              disabled: true
            },
            {
              compKey: 'compKey25',
              labelName: '维修类型：',
              codeField: 'partRepairType',
              isRequire: true,
              component: () => import('@/components/org/commonSelect'),
              customObj: {
                api: orgApis.sparePartsListQuerycategoryselect
              },
              type: 'dropdownList',
              isMust: true
            },
            {
              compKey: 'compKey4',
              labelName: '供应商: ',
              codeField: 'supplierFullName',
              component: () => import('@/components/ve/multipleSearchInput'),
              type: 'inputText',
              isRequire: true,
              isMul: false,
              isMust: true,
              dataToObject: {
                // 控制弹窗页面 搜索 网格 元素
                tableComponents: [
                  {
                    compKey: 'compKey1',
                    labelName: '供应商编码：',
                    codeField: 'supplierCode',
                    component: () => import('@/components/org/commonInput'),
                    type: 'inputText',
                    isMust: true,
                    span: 8
                  },
                  {
                    compKey: 'compKey2',
                    labelName: '供应商简称：',
                    codeField: 'supplierShortName',
                    component: () => import('@/components/org/commonInput'),
                    type: 'inputText',
                    isMust: true,
                    span: 8
                  },
                  {
                    compKey: 'compKey3',
                    labelName: '状态：',
                    codeField: 'isEnable',
                    component: () => import('@/components/org/LookupValue'),
                    oFields: 'statusId,statusText',
                    options: [
                      { statusId: '1', statusText: '启用' },
                      { statusId: '0', statusText: '禁用' }
                    ],
                    type: 'dropdownList',
                    isMust: true,
                    span: 8
                  }
                ],
                tableCols: [
                  {
                    prop: 'supplierCode',
                    label: '供应商编码'
                  },
                  {
                    prop: 'supplierShortName',
                    label: '供应商简称'
                  },
                  {
                    prop: 'supplierFullName',
                    label: '供应商全称'
                  },
                  {
                    prop: 'supplierAddr',
                    label: '详细地址'
                  },
                  {
                    prop: 'linkMan',
                    label: '联系人'
                  },
                  {
                    prop: 'linkManTel',
                    label: '联系电话'
                  },
                  {
                    prop: 'isEnableCn',
                    label: '状态'
                  },
                  {
                    prop: 'remark',
                    label: '备注'
                  }
                ],
                returnField: {
                  prop: 'supplierFullName',
                  label: '供应商全称'
                },
                api: orgApis.supplierInformationQueryquery
              }
            },
            {
              compKey: 'compKey8',
              labelName: '最小起订数量：',
              codeField: 'minOrderAmount',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              validrule: {
                isInteger: true
              },
              isRequire: true,
              isMust: true
            },

            {
              compKey: 'compKey9',
              labelName: '最小销售数量：',
              codeField: 'minSaleAmount',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isRequire: true,
              validrule: {
                isInteger: true
              },
              isMust: true
            },
            {
              compKey: 'compKey5',
              labelName: '备件状态：',
              codeField: 'partStatus',
              component: () => import('@/components/org/commonSelect'),
              customObj: {
                api: orgApis.sparePartsListQuerystateselect
              },
              type: 'dropdownList',
              // isRequire: true,
              isMust: true
            },

            {
              compKey: 'compKey6',
              labelName: '销售开关：',
              codeField: 'isSale',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'statusId,statusText',
              options: [
                { statusId: '1', statusText: '开放' },
                { statusId: '0', statusText: '关闭' }
              ],
              type: 'dropdownList',
              isRequire: false,
              isMust: true
            },

            {
              compKey: 'compKey17',
              labelName: '订货开关：',
              codeField: 'isOrder',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'statusId,statusText',
              options: [
                { statusId: '1', statusText: '开放' },
                { statusId: '0', statusText: '关闭' }
              ],
              type: 'dropdownList',
              isRequire: false,
              isMust: true
            },

            // {
            //   compKey: 'compKey7',
            //   labelName: 'SNP：',
            //   codeField: 'snp',   
            //   component: () => import('@/components/org/commonInput'),
            //   type: 'inputText',
            //   isRequire: false,
            //   validrule: {
            //     isInteger: true
            //   },
            //   isMust: true
            // },

            
            {
              compKey: 'compKey10',
              labelName: '订货周期：',
              codeField: 'orderPeriod',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              validrule: {
                resectMessage: {
                  isformat: '请输入正确的周期!'
                },
                isformat: /^([0-9]*|(-?\d+)(\.\d+)?)$/
              },
              isRequire: false,
              isMust: true
            },

            {
              compKey: 'compKey11',
              labelName: '到货周期：',
              codeField: 'arrivalPeriod',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              validrule: {
                resectMessage: {
                  isformat: '请输入正确的周期!'
                },
                isformat: /^([0-9]*|(-?\d+)(\.\d+)?)$/
              },
              isRequire: false,
              isMust: true
            },

            {
              compKey: 'compKey12',
              labelName: '规格参数：',
              codeField: 'specifications',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },

            {
              compKey: 'compKey13',
              labelName: '产地：',
              codeField: 'origin',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },

            {
              compKey: 'compKey14',
              labelName: '允许外购：',
              codeField: 'isOutsourcing',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'statusId,statusText',
              options: [
                { statusId: '1', statusText: '是' },
                { statusId: '0', statusText: '否' }
              ],
              type: 'dropdownList',
              isMust: true
            },

            {
              compKey: 'compKey15',
              labelName: '追溯件：',
              codeField: 'isRetrospect',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'statusId,statusText',
              options: [
                { statusId: '1', statusText: '是' },
                { statusId: '0', statusText: '否' }
              ],
              type: 'dropdownList',
              isMust: true
            },

            {
              compKey: 'compKey16',
              labelName: '三电标识：',
              codeField: 'bmcCode',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'statusId,statusText',
              options: [
                { statusId: '1', statusText: '电机' },
                { statusId: '2', statusText: '电池' },
                { statusId: '3', statusText: '中控' }
              ],
              type: 'dropdownList',
              isMust: true
            },

            {
              compKey: 'compKey18',
              labelName: '质保期(月)：',
              codeField: 'warrantyPeriod',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },

            {
              compKey: 'compKey19',
              labelName: '是否3C件：',
              codeField: 'is3c',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'statusId,statusText',
              options: [
                { statusId: '1', statusText: '是' },
                { statusId: '0', statusText: '否' }
              ],
              type: 'dropdownList',
              isMust: true
            },

            {
              compKey: 'compKey20',
              labelName: '3C件类型：',
              codeField: 'c3PieceType',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },

            {
              compKey: 'compKey21',
              labelName: '直送件：',
              codeField: 'directDeliveryPiece',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'statusId,statusText',
              options: [
                { statusId: '1', statusText: '是' },
                { statusId: '0', statusText: '否' }
              ],
              type: 'dropdownList',
              isMust: true
            },

            {
              compKey: 'compKey22',
              labelName: 'VIN件：',
              codeField: 'vinPiece',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'statusId,statusText',
              options: [
                { statusId: '1', statusText: '是' },
                { statusId: '0', statusText: '否' }
              ],
              type: 'dropdownList',
              isMust: true
            },

            {
              compKey: 'compKey23',
              labelName: '运输限制：',
              codeField: 'transportRestrictions',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'statusId,statusText',
              options: [
                { statusId: '1', statusText: '是' },
                { statusId: '0', statusText: '否' }
              ],
              type: 'dropdownList',
              isMust: true
            },

            {
              compKey: 'compKey24',
              labelName: '重点件：',
              codeField: 'keyPiece',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'statusId,statusText',
              options: [
                { statusId: '1', statusText: '是' },
                { statusId: '0', statusText: '否' }
              ],
              type: 'dropdownList',
              isMust: true
            },

            {
              compKey: 'compKey3',
              labelName: '英文名称：',
              codeField: 'partNameEn',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },
            // {
            //   compKey: 'compKey26',
            //   labelName: '替换件: ',
            //   codeField: 'replacePiece',
            //   component: () => import('@/components/ve/carStockHouse'),
            //   // oFields: 'statusId,statusText',
            //   // options: [
            //   //   { statusId: '1', statusText: '厂家备件' },
            //   //   { statusId: '0', statusText: '网点备件' }
            //   // ],
            //   type: 'dropdownList',
            //   isMul: false,
            //   isMust: true
            // },
            {
              compKey: 'compKey26',
              labelName: '替换件：',
              codeField: 'replacePiece',
              textField: 'replacePieceCn',
              component: () => import('@/views/ve/planReport/components/multipleSearchInput'),
              // isRequire: true,
              isMul: false,
              isMust: true,
              customObj: {showType: 'text'},
              dataToObject: {
                oFields: 'partNo,partName',
                // 控制弹窗页面 搜索 网格 元素
                unSelect: { field: 'partNo', values: [], message: '替换件不能和原备件相同！' },
                tableComponents: [
                  {
                    compKey: 'compKey1',
                    labelName: '替换件编码：',
                    codeField: 'partNo',
                    component: () => import('@/components/org/commonInput'),
                    type: 'inputText',
                    isMust: true,
                    span: 8
                  },
                  {
                    compKey: 'compKey2',
                    labelName: '替换件名称：',
                    codeField: 'partName',
                    component: () => import('@/components/org/commonInput'),
                    type: 'inputText',
                    isMust: true,
                    span: 8
                  }
                ],
                tableCols: [
                  {
                    prop: 'partNo',
                    label: '替换件编码'
                  },
                  {
                    prop: 'partName',
                    label: '替换件名称'
                  }
                ],
                formField: {
                  partNo: '',
                  partName: ''
                },
                returnField: {
                  prop: 'partNo',
                  label: '替换件编码'
                },
                api: {
                  APIUrl: '/mp/e3sh/spareparts/oemSparePartsListMaintainService/queryOemSparePartsList.do'
                }
              }
            },

            {
              compKey: 'compKey27',
              labelName: 'ABC分类：',
              codeField: 'abcType',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'statusId,statusText',
              options: [
                { statusId: 'C', statusText: 'C类' },
                { statusId: 'B', statusText: 'B类' },
                { statusId: 'A', statusText: 'A类' }
              ],
              type: 'dropdownList',
              isMust: true
            },

            {
              compKey: 'compKey28',
              labelName: '特殊件：',
              codeField: 'specialPiece',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'statusId,statusText',
              options: [
                { statusId: '1', statusText: '是' },
                { statusId: '0', statusText: '否' }
              ],
              type: 'dropdownList',
              isMust: true
            },

            {
              compKey: 'compKey29',
              labelName: '适用车型：',
              codeField: 'carConfigIdList',
              textField: 'carConfigCn',
              component: () => import('@/views/ve/planReport/components/multipleSearchInput'),
              isMul: false,
              isMust: true,
              customObj: {showType: 'code'},
              dataToObject: {
                oFields: 'carConfigCode,carConfigCn',
                // 控制弹窗页面 搜索 网格 元素
                tableComponents: [
                  {
                    compKey: 'compKey1',
                    labelName: '车型编码：',
                    codeField: 'carConfigCode',
                    component: () => import('@/components/org/commonInput'),
                    type: 'inputText',
                    isMust: true,
                    span: 8
                  },
                  {
                    compKey: 'compKey2',
                    labelName: '车型名称：',
                    codeField: 'carConfigCn',
                    component: () => import('@/components/org/commonInput'),
                    type: 'inputText',
                    isMust: true,
                    span: 8
                  }
                ],
                tableCols: [
                  {
                    prop: 'carConfigCode',
                    label: '车型编码'
                  },
                  {
                    prop: 'carConfigCn',
                    label: '车型名称'
                  },
                  {
                    prop: 'carSeriesCode',
                    label: '车系编码'
                  },
                  {
                    prop: 'carSeriesCn',
                    label: '车系名称'
                  }
                ],
                formField: {
                  carConfigCode: '',
                  carConfigCn: ''
                },
                returnField: {
                  prop: 'carConfigCn',
                  label: '车型名称'
                },
                api: {
                  APIUrl: 'mp/e3sh/spareparts/basic/partPriceQueryService/queryCarConfigCodes.do'
                }
              }
            },

            {
              compKey: 'compKey30',
              labelName: '不返件：',
              codeField: 'isNotReturn',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'statusId,statusText',
              options: [
                { statusId: '1', statusText: '是' },
                { statusId: '0', statusText: '否' }
              ],
              type: 'dropdownList',
              isMust: true
            },
            {
              compKey: 'compKey32',
              labelName: '备件单位：',
              codeField: 'unit',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },
            {
              compKey: 'compKey31',
              labelName: '备注：',
              codeField: 'remark',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true,
              span: 24
            },

            {
              compKey: 'compKey32',
              labelName: '配件ID',
              codeField: 'partId',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: false,
              span: 12
            }
          ],

      // 标题
      textMap: {
        edit: '备件信息修改',
        add: '备件信息修改'
      },

      // 表单数据（无需赋值，由混入对象赋值）
      formField: this.$utils.getFormField(
        this,
        {
          partId: '',
          partNo: '',
          partName: '',

          supplierFullName: '',
          partStatus: '',
          isSale: '',
          isOrder: '',
          // snp: '',
          partNameEn: '',
          minOrderAmount: '',
          minSaleAmount: '',
          orderPeriod: '',
          arrivalPeriod: '',
          specifications: '',
          origin: '',
          isOutsourcing: '',
          isRetrospect: '',
          bmcCode: '',
          warrantyPeriod: '',
          is3c: '',
          c3PieceType: '',
          directDeliveryPiece: '',
          vinPiece: '',
          transportRestrictions: '',
          keyPiece: '',
          partRepairType: '',
          replacePiece: '',
          replacePieceCn: '',
          abcType: '',
          specialPiece: '',
          carConfigIdList: '',
          carConfigCn: '',
          isNotReturn: '',
          remark: '',
          areaRemark: '',
          updateControlId: '',
          unit: '',
        },
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode]
      )
    }
  },
  mounted() {
    this.tableComponents.find(o => o.codeField === 'replacePiece').dataToObject.unSelect.values = [this.formField.partNo]
  },
  watch:{     
    // 'formField.snp':{
    //   deep:true,
    //   handler:function(val){
    //     if(!val)return
    //     if(!this.formField.minOrderAmount){
    //       this.$message({
    //         message:'填写SNP之前请填写最小起订数量',
    //         type:'error'
    //       })
    //       return
    //     }
    //    if(val % this.formField.minOrderAmount){         
    //      this.$message({
    //        message:"SNP必须是最小起订数量的整数倍"
    //      })
    //      this.formField.minOrderAmount = null
    //    }
    //   }
    // }
  },
  methods: {
    // 关闭弹窗
    // handleClose(done) {
    //   this.$confirm('还未保存，确认关闭窗口？')
    //     .then(_ => {
    //       this.$emit('update:curPopupsVisible', false)
    //       done()
    //     })
    //     .catch(_ => {})
    // },
    closeEdit() {
      this.$emit('close', false)
      this.curPopupsVisible = false
    },
    // 新增/编辑保存按钮
    save() {
      const that = this
      this.$utils.validataMoth(this, 'validpopup')
      // if(this.formField.snp % this.formField.minOrderAmount){
      //    this.$message({
      //      message:"SNP必须是最小起订数量的整数倍"
      //    })
      //    this.formField.snp = null
      //    return 
      // }
      if (this.valid) {
        if (that.popupsState === 'edit') {
          that.editSave()
        } else {
          that.addSave()
        }
      }
    },

    // 编辑保存
    editSave() {
      const that = this      
      const queryObj = {
        // 保存mutation
        type: 'mutation',
        // api配置
        apiConfig: orgApis.sparePartsListMaintenancesave,
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: that.formField
      }
      // 转换了中台请求格式数据
      var paramD = this.$getParams(queryObj)
      // 调用中台API方法（可复用）
      this.$requestAPI(paramD).then((response) => {
        if (response.result === '1') {
          this.$message({
            message: this.$t('sys.tips.esTip5') /* 保存成功*/,
            type: 'success',
            duration: 1000
          })
          this.closeEdit()
          // this.curPopupsVisible = false
          this.$nextTick(() => {
            this.$parent.queryTable(1)
          })
        } else {
          // 保存失败
          this.$message({
            message: '保存失败:' + response.msg,
            type: 'warn',
            uration: 1000
          })
        }
        // this.curPopupsVisible = false
      })
    },

    // 新增保存
    addSave() {
      const that = this
      const queryObj = {
        // 保存mutation
        type: 'mutation',
        // api配置
        apiConfig: {
          ServiceCode: 'addBigArea',
          APIUrl: '/mp/e3sh/basics/ve/bigareaService/addBigArea.do'
        },
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: that.formField
      }
      // 转换了中台请求格式数据
      var paramD = this.$getParams(queryObj)
      // 调用中台API方法（可复用）
      this.$requestAPI(paramD).then((response) => {
        if (response.result === '1') {
          this.$message({
            message: '添加成功',
            type: 'success',
            duration: 1000
          })
          this.curPopupsVisible = false
          this.$nextTick(() => {
            this.$parent.queryTable(1)
          })
        } else {
          // 保存失败
          this.$message({
            message: '添加失败:' + response.msg,
            type: 'warn',
            uration: 1000
          })
        }
        this.curPopupsVisible = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.promptSpan {
  position: absolute;
  bottom: 26px;
  right: 335px;
  color: #e70020;
}
</style>

